﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tablet
{
    public partial class _Default : System.Web.UI.Page
    {
        protected string sLines = string.Empty;
        protected string lnkBanner = string.Empty;
        protected string imgBanner = "none";

        protected void Page_Load(object sender, EventArgs e)
        {
            LocalFunctions FUNC = new LocalFunctions();

            int TotalResults = 0;
            List<Application> lstTopApps = FUNC.GetPopularApps(0, 10, out TotalResults);

            Random rnd = new Random();

            var randomList = from item in lstTopApps
                             orderby rnd.Next()
                             select item;

            lstTopApps = randomList.ToList<Application>().GetRange(0, 3);

            rptMainContent.DataSource = lstTopApps;
            rptMainContent.DataBind();


            DataSet DSCategories = FUNC.GetCategories();
            if (DSCategories != null)
            {
                if (DSCategories.Tables[0] != null)
                    if (DSCategories.Tables[0].Rows.Count > 0)
                    {
                        var TopCategoriesList = from item in DSCategories.Tables[0].AsEnumerable().Select(
                                               d => new Category
                                               {
                                                   id = Convert.ToInt32(d["id"]),
                                                   parentid = Convert.ToInt32((d["parentid"] == string.Empty) ? "0" : d["parentid"]),
                                                   name = d["name"].ToString()
                                               })
                                                select item;


                        List<Category> Cats = new List<Category>();
                        for (int i = 0; i < TopCategoriesList.ToList<Category>().Count; i++)
                        {

                            Category c = TopCategoriesList.ToList<Category>()[i];
                            if (c.id == 19)
                                c.link = "http://3g.orange.co.il/3gorange/default.aspx?id=1072504";
                            else
                                c.link = string.Format("Category.aspx?id={0}", c.id);

                            Cats.Add(c);
                        }


                        #region First
                        rptTopCategories.DataSource = Cats.GetRange(0, 3);
                        rptTopCategories.DataBind();
                        #endregion

                        #region Second
                        rptCategories.DataSource = Cats.GetRange(3, 9);
                        rptCategories.DataBind();
                        #endregion

                        #region Third

                        Cats = Cats.GetRange(12, Cats.Count - 12);
                        rptLnkCats.DataSource = Cats;
                        rptLnkCats.DataBind();
                        #endregion

                        //double dNumItems = (double)lstCat.Count / 4;
                        //dNumItems = Math.Round(dNumItems, MidpointRounding.ToEven);
                        //for (int d = 0; d < 4 - dNumItems; d++)
                        //{
                        //    sLines += "<li><div class=\"inner-cut horizontal\">" +
                        //            "<span class=\"shadow\"></span>" +
                        //            "<span class=\"highlight\"></span>" +
                        //            "</div><!-- inner-cut -->" +
                        //            "<div class=\"inner-cut vertical\">" +
                        //            "<span class=\"shadow\"></span>" +
                        //            "<span class=\"highlight\"></span>" +
                        //            "</div><!-- inner-cut --></li>";
                        //}

                    }
            }
        }
    }
}
